
import { Context } from 'koa';
import { validateDto } from '@/utils/validateDto';
import { CreateBannerDto, UpdateBannerDto, QueryBannerDto, DeleteBannerDto, SelectOneBannerDto,SortBannerDto } from '@/dto/banner.dto';
import bannerService from '@/services/banner.service';

class BannerController {
    async selectList(ctx: Context) {

        const reqData = ctx.request.body as QueryBannerDto;
        const errors = await validateDto(QueryBannerDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        let resData = await bannerService.selectList(reqData)
        ctx.body = {
            code: 200,
            message: "列表查询成功",
            data:resData
        };
    }
    async selectOne(ctx: Context) {
        const reqData = ctx.request.body as SelectOneBannerDto;
        let resData = await bannerService.selectOne(reqData)

        
        ctx.body = {
            code: 200,
            message:"详情查询成功",
            data: resData,

        };
    }

    async add(ctx: Context) {
        const userId = ctx.state.userId as number;
        const reqData = ctx.request.body as CreateBannerDto;
        //reqData.userId = reqData.userId || userId; //暂时注释
        const errors = await validateDto(CreateBannerDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await bannerService.add(reqData);   
        
        ctx.body = {
        code: 200,
        message:"添加成功",
        data:{id:resData.id},
        };

    }

    async update(ctx: Context) {
        const reqData = <UpdateBannerDto>ctx.request.body
        const errors = await validateDto(UpdateBannerDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await bannerService.update(reqData);   


        ctx.body = {
            code: 200,
            message:"更新成功",
            data: {},
        };

    }
    async delete(ctx: Context) {
        const reqData = ctx.request.body as DeleteBannerDto;
        const errors = await validateDto(DeleteBannerDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await bannerService.delete(reqData);

        ctx.body = {
            code: 200,
            message:"删除成功",
            data: {},
        };

    }
    async sort(ctx: Context) {
        const reqData = ctx.request.body as SortBannerDto;
        const errors = await validateDto(SortBannerDto, reqData);
        if (errors) {
            ctx.body = errors;
            return
        }
        const resData = await bannerService.sort(reqData);
    
        ctx.body = {
            code: 200,
            message:"排序成功",
            data: {},
        };
    
    }

}

export default new BannerController();   
    
    